package com.ssfeng.youxia.config;

import com.ssfeng.youxia.bean.JacksonObjectMapper;
import com.ssfeng.youxia.exceptions.YouXiaHandlerExceptionResolver;
import com.ssfeng.youxia.interceptors.BaseInterceptor;
import com.ssfeng.youxia.interceptors.CrossInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

@EnableWebMvc
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@ComponentScan({ "com.ssfeng.youxia.controller",
		"com.ssfeng.youxia.commons",
		"com.ssfeng.youxia.listener",
		"com.ssfeng.youxia.event"
		,"com.ssfeng.youxia.publisher",
		"com.ssfeng.youxia.SpringProcessor"})
public class SpringWebConfig extends WebMvcConfigurerAdapter {
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
		registry.addResourceHandler("/image/**").addResourceLocations("/image/");
		registry.addResourceHandler("/js/**").addResourceLocations("/js/");
		registry.addResourceHandler("/css/**").addResourceLocations("/css/");
		registry.addResourceHandler("/layui/**").addResourceLocations("/layui/");
		registry.addResourceHandler("/error/**").addResourceLocations("/error/");
		registry.addResourceHandler("/**/*.html").addResourceLocations("/");

	}

	/*
	 * @Bean public ViewResolver resourceBundleViewResolver() {
	 * ResourceBundleViewResolver bean = new ResourceBundleViewResolver();
	 * bean.setBasename("views"); return bean; }
	 */

	/*@Bean
	public InternalResourceViewResolver viewResolver() {
		InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
		viewResolver.setViewClass(JstlView.class);
		viewResolver.setPrefix("/WEB-INF/views/jsp/");
		viewResolver.setSuffix(".jsp");
		viewResolver.setOrder(2);
		return viewResolver;
	}*/

	@Bean
	public FreeMarkerViewResolver freemarkerViewResolver() {
		FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
		resolver.setCache(true);
		resolver.setContentType("text/html;charset=UTF-8");
		// resolver.setPrefix("");
		resolver.setSuffix(".ftl");
		resolver.setOrder(1);
		return resolver;
	}

	@Bean
	public FreeMarkerConfigurer freemarkerConfig() {
		FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
		freeMarkerConfigurer.setDefaultEncoding("utf-8");
		freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/views/");
		return freeMarkerConfigurer;
	}

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		super.addInterceptors(registry);
		// 定义拦截什么请求
		registry.addInterceptor(new BaseInterceptor()).addPathPatterns("/**");
		registry.addInterceptor(new CrossInterceptor()).addPathPatterns("/**");
	}

	/**
	 * 定义文件上传解析器
	 * @return
	 */
	@Bean
	public MultipartResolver multipartResolver() {
		return new StandardServletMultipartResolver();
	}

	/****
	 * 异常处理
	 * @return
	 */
	@Bean
	public HandlerExceptionResolver handlerExceptionResolver() {
		return new YouXiaHandlerExceptionResolver();
	}

	/***
	 * 前后端消息转换器
	 * @param jacksonObjectMapper
	 * @return
	 */
	@Autowired
	@Bean
	public MappingJackson2JsonView mappingJackson2JsonView(JacksonObjectMapper jacksonObjectMapper) {
		MappingJackson2JsonView mappingJackson2JsonView = new MappingJackson2JsonView();
		mappingJackson2JsonView.setObjectMapper(jacksonObjectMapper);
		return mappingJackson2JsonView;
	}

	/***
	 * 跨域处理,尚未测试验证
	 * @param registry
	 */
	@Override
	public void addCorsMappings(CorsRegistry registry) {
		registry.addMapping("/**")
				.allowedOrigins("http://localhost:9000", "null")
				.allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
				.maxAge(3600)
				.allowCredentials(true);
	}
}
